﻿using System;
using System.Collections.Generic;

namespace UniRx.Operators
{
	// Token: 0x020002EC RID: 748
	internal class SelectManyObservable<TSource, TResult> : OperatorObservableBase<TResult>
	{
		// Token: 0x06000FDA RID: 4058 RVA: 0x000493D4 File Offset: 0x000477D4
		public SelectManyObservable(IObservable<TSource> source, Func<TSource, IObservable<TResult>> selector) : base(source.IsRequiredSubscribeOnCurrentThread<TSource>())
		{
			this.source = source;
			this.selector = selector;
		}

		// Token: 0x06000FDB RID: 4059 RVA: 0x000493F0 File Offset: 0x000477F0
		public SelectManyObservable(IObservable<TSource> source, Func<TSource, int, IObservable<TResult>> selector) : base(source.IsRequiredSubscribeOnCurrentThread<TSource>())
		{
			this.source = source;
			this.selectorWithIndex = selector;
		}

		// Token: 0x06000FDC RID: 4060 RVA: 0x0004940C File Offset: 0x0004780C
		public SelectManyObservable(IObservable<TSource> source, Func<TSource, IEnumerable<TResult>> selector) : base(source.IsRequiredSubscribeOnCurrentThread<TSource>())
		{
			this.source = source;
			this.selectorEnumerable = selector;
		}

		// Token: 0x06000FDD RID: 4061 RVA: 0x00049428 File Offset: 0x00047828
		public SelectManyObservable(IObservable<TSource> source, Func<TSource, int, IEnumerable<TResult>> selector) : base(source.IsRequiredSubscribeOnCurrentThread<TSource>())
		{
			this.source = source;
			this.selectorEnumerableWithIndex = selector;
		}

		// Token: 0x06000FDE RID: 4062 RVA: 0x00049444 File Offset: 0x00047844
		protected override IDisposable SubscribeCore(IObserver<TResult> observer, IDisposable cancel)
		{
			if (this.selector != null)
			{
				return new SelectManyObservable<TSource, TResult>.SelectManyOuterObserver(this, observer, cancel).Run();
			}
			if (this.selectorWithIndex != null)
			{
				return new SelectManyObservable<TSource, TResult>.SelectManyObserverWithIndex(this, observer, cancel).Run();
			}
			if (this.selectorEnumerable != null)
			{
				return new SelectManyObservable<TSource, TResult>.SelectManyEnumerableObserver(this, observer, cancel).Run();
			}
			if (this.selectorEnumerableWithIndex != null)
			{
				return new SelectManyObservable<TSource, TResult>.SelectManyEnumerableObserverWithIndex(this, observer, cancel).Run();
			}
			throw new InvalidOperationException();
		}

		// Token: 0x0400093E RID: 2366
		private readonly IObservable<TSource> source;

		// Token: 0x0400093F RID: 2367
		private readonly Func<TSource, IObservable<TResult>> selector;

		// Token: 0x04000940 RID: 2368
		private readonly Func<TSource, int, IObservable<TResult>> selectorWithIndex;

		// Token: 0x04000941 RID: 2369
		private readonly Func<TSource, IEnumerable<TResult>> selectorEnumerable;

		// Token: 0x04000942 RID: 2370
		private readonly Func<TSource, int, IEnumerable<TResult>> selectorEnumerableWithIndex;

		// Token: 0x020002ED RID: 749
		private class SelectManyOuterObserver : OperatorObserverBase<TSource, TResult>
		{
			// Token: 0x06000FDF RID: 4063 RVA: 0x000494BA File Offset: 0x000478BA
			public SelectManyOuterObserver(SelectManyObservable<TSource, TResult> parent, IObserver<TResult> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06000FE0 RID: 4064 RVA: 0x000494D8 File Offset: 0x000478D8
			public IDisposable Run()
			{
				this.collectionDisposable = new CompositeDisposable();
				this.sourceDisposable = new SingleAssignmentDisposable();
				this.collectionDisposable.Add(this.sourceDisposable);
				this.sourceDisposable.Disposable = this.parent.source.Subscribe(this);
				return this.collectionDisposable;
			}

			// Token: 0x06000FE1 RID: 4065 RVA: 0x00049530 File Offset: 0x00047930
			public override void OnNext(TSource value)
			{
				IObservable<TResult> observable;
				try
				{
					observable = this.parent.selector(value);
				}
				catch (Exception error)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
					return;
				}
				SingleAssignmentDisposable singleAssignmentDisposable = new SingleAssignmentDisposable();
				this.collectionDisposable.Add(singleAssignmentDisposable);
				SelectManyObservable<TSource, TResult>.SelectManyOuterObserver.SelectMany observer = new SelectManyObservable<TSource, TResult>.SelectManyOuterObserver.SelectMany(this, singleAssignmentDisposable);
				singleAssignmentDisposable.Disposable = observable.Subscribe(observer);
			}

			// Token: 0x06000FE2 RID: 4066 RVA: 0x000495B8 File Offset: 0x000479B8
			public override void OnError(Exception error)
			{
				object obj = this.gate;
				lock (obj)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x06000FE3 RID: 4067 RVA: 0x00049614 File Offset: 0x00047A14
			public override void OnCompleted()
			{
				this.isStopped = true;
				if (this.collectionDisposable.Count == 1)
				{
					object obj = this.gate;
					lock (obj)
					{
						try
						{
							this.observer.OnCompleted();
						}
						finally
						{
							base.Dispose();
						}
					}
				}
				else
				{
					this.sourceDisposable.Dispose();
				}
			}

			// Token: 0x04000943 RID: 2371
			private readonly SelectManyObservable<TSource, TResult> parent;

			// Token: 0x04000944 RID: 2372
			private CompositeDisposable collectionDisposable;

			// Token: 0x04000945 RID: 2373
			private SingleAssignmentDisposable sourceDisposable;

			// Token: 0x04000946 RID: 2374
			private object gate = new object();

			// Token: 0x04000947 RID: 2375
			private bool isStopped;

			// Token: 0x020002EE RID: 750
			private class SelectMany : OperatorObserverBase<TResult, TResult>
			{
				// Token: 0x06000FE4 RID: 4068 RVA: 0x00049698 File Offset: 0x00047A98
				public SelectMany(SelectManyObservable<TSource, TResult>.SelectManyOuterObserver parent, IDisposable cancel) : base(parent.observer, cancel)
				{
					this.parent = parent;
					this.cancel = cancel;
				}

				// Token: 0x06000FE5 RID: 4069 RVA: 0x000496B8 File Offset: 0x00047AB8
				public override void OnNext(TResult value)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						this.observer.OnNext(value);
					}
				}

				// Token: 0x06000FE6 RID: 4070 RVA: 0x00049704 File Offset: 0x00047B04
				public override void OnError(Exception error)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						try
						{
							this.observer.OnError(error);
						}
						finally
						{
							base.Dispose();
						}
					}
				}

				// Token: 0x06000FE7 RID: 4071 RVA: 0x00049768 File Offset: 0x00047B68
				public override void OnCompleted()
				{
					this.parent.collectionDisposable.Remove(this.cancel);
					if (this.parent.isStopped && this.parent.collectionDisposable.Count == 1)
					{
						object gate = this.parent.gate;
						lock (gate)
						{
							try
							{
								this.observer.OnCompleted();
							}
							finally
							{
								base.Dispose();
							}
						}
					}
				}

				// Token: 0x04000948 RID: 2376
				private readonly SelectManyObservable<TSource, TResult>.SelectManyOuterObserver parent;

				// Token: 0x04000949 RID: 2377
				private readonly IDisposable cancel;
			}
		}

		// Token: 0x020002EF RID: 751
		private class SelectManyObserverWithIndex : OperatorObserverBase<TSource, TResult>
		{
			// Token: 0x06000FE8 RID: 4072 RVA: 0x00049808 File Offset: 0x00047C08
			public SelectManyObserverWithIndex(SelectManyObservable<TSource, TResult> parent, IObserver<TResult> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06000FE9 RID: 4073 RVA: 0x00049824 File Offset: 0x00047C24
			public IDisposable Run()
			{
				this.collectionDisposable = new CompositeDisposable();
				this.sourceDisposable = new SingleAssignmentDisposable();
				this.collectionDisposable.Add(this.sourceDisposable);
				this.sourceDisposable.Disposable = this.parent.source.Subscribe(this);
				return this.collectionDisposable;
			}

			// Token: 0x06000FEA RID: 4074 RVA: 0x0004987C File Offset: 0x00047C7C
			public override void OnNext(TSource value)
			{
				IObservable<TResult> observable;
				try
				{
					observable = this.parent.selectorWithIndex(value, this.index++);
				}
				catch (Exception error)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
					return;
				}
				SingleAssignmentDisposable singleAssignmentDisposable = new SingleAssignmentDisposable();
				this.collectionDisposable.Add(singleAssignmentDisposable);
				SelectManyObservable<TSource, TResult>.SelectManyObserverWithIndex.SelectMany observer = new SelectManyObservable<TSource, TResult>.SelectManyObserverWithIndex.SelectMany(this, singleAssignmentDisposable);
				singleAssignmentDisposable.Disposable = observable.Subscribe(observer);
			}

			// Token: 0x06000FEB RID: 4075 RVA: 0x00049918 File Offset: 0x00047D18
			public override void OnError(Exception error)
			{
				object obj = this.gate;
				lock (obj)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x06000FEC RID: 4076 RVA: 0x00049974 File Offset: 0x00047D74
			public override void OnCompleted()
			{
				this.isStopped = true;
				if (this.collectionDisposable.Count == 1)
				{
					object obj = this.gate;
					lock (obj)
					{
						try
						{
							this.observer.OnCompleted();
						}
						finally
						{
							base.Dispose();
						}
					}
				}
				else
				{
					this.sourceDisposable.Dispose();
				}
			}

			// Token: 0x0400094A RID: 2378
			private readonly SelectManyObservable<TSource, TResult> parent;

			// Token: 0x0400094B RID: 2379
			private CompositeDisposable collectionDisposable;

			// Token: 0x0400094C RID: 2380
			private int index;

			// Token: 0x0400094D RID: 2381
			private object gate = new object();

			// Token: 0x0400094E RID: 2382
			private bool isStopped;

			// Token: 0x0400094F RID: 2383
			private SingleAssignmentDisposable sourceDisposable;

			// Token: 0x020002F0 RID: 752
			private class SelectMany : OperatorObserverBase<TResult, TResult>
			{
				// Token: 0x06000FED RID: 4077 RVA: 0x000499F8 File Offset: 0x00047DF8
				public SelectMany(SelectManyObservable<TSource, TResult>.SelectManyObserverWithIndex parent, IDisposable cancel) : base(parent.observer, cancel)
				{
					this.parent = parent;
					this.cancel = cancel;
				}

				// Token: 0x06000FEE RID: 4078 RVA: 0x00049A18 File Offset: 0x00047E18
				public override void OnNext(TResult value)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						this.observer.OnNext(value);
					}
				}

				// Token: 0x06000FEF RID: 4079 RVA: 0x00049A64 File Offset: 0x00047E64
				public override void OnError(Exception error)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						try
						{
							this.observer.OnError(error);
						}
						finally
						{
							base.Dispose();
						}
					}
				}

				// Token: 0x06000FF0 RID: 4080 RVA: 0x00049AC8 File Offset: 0x00047EC8
				public override void OnCompleted()
				{
					this.parent.collectionDisposable.Remove(this.cancel);
					if (this.parent.isStopped && this.parent.collectionDisposable.Count == 1)
					{
						object gate = this.parent.gate;
						lock (gate)
						{
							try
							{
								this.observer.OnCompleted();
							}
							finally
							{
								base.Dispose();
							}
						}
					}
				}

				// Token: 0x04000950 RID: 2384
				private readonly SelectManyObservable<TSource, TResult>.SelectManyObserverWithIndex parent;

				// Token: 0x04000951 RID: 2385
				private readonly IDisposable cancel;
			}
		}

		// Token: 0x020002F1 RID: 753
		private class SelectManyEnumerableObserver : OperatorObserverBase<TSource, TResult>
		{
			// Token: 0x06000FF1 RID: 4081 RVA: 0x00049B68 File Offset: 0x00047F68
			public SelectManyEnumerableObserver(SelectManyObservable<TSource, TResult> parent, IObserver<TResult> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06000FF2 RID: 4082 RVA: 0x00049B79 File Offset: 0x00047F79
			public IDisposable Run()
			{
				return this.parent.source.Subscribe(this);
			}

			// Token: 0x06000FF3 RID: 4083 RVA: 0x00049B8C File Offset: 0x00047F8C
			public override void OnNext(TSource value)
			{
				IEnumerable<TResult> enumerable;
				try
				{
					enumerable = this.parent.selectorEnumerable(value);
				}
				catch (Exception error)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
					return;
				}
				using (IEnumerator<TResult> enumerator = enumerable.GetEnumerator())
				{
					bool flag = true;
					while (flag)
					{
						flag = false;
						TResult value2 = default(TResult);
						try
						{
							flag = enumerator.MoveNext();
							if (flag)
							{
								value2 = enumerator.Current;
							}
						}
						catch (Exception error2)
						{
							try
							{
								this.observer.OnError(error2);
							}
							finally
							{
								base.Dispose();
							}
							break;
						}
						if (flag)
						{
							this.observer.OnNext(value2);
						}
					}
				}
			}

			// Token: 0x06000FF4 RID: 4084 RVA: 0x00049C98 File Offset: 0x00048098
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000FF5 RID: 4085 RVA: 0x00049CD0 File Offset: 0x000480D0
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x04000952 RID: 2386
			private readonly SelectManyObservable<TSource, TResult> parent;
		}

		// Token: 0x020002F2 RID: 754
		private class SelectManyEnumerableObserverWithIndex : OperatorObserverBase<TSource, TResult>
		{
			// Token: 0x06000FF6 RID: 4086 RVA: 0x00049D08 File Offset: 0x00048108
			public SelectManyEnumerableObserverWithIndex(SelectManyObservable<TSource, TResult> parent, IObserver<TResult> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06000FF7 RID: 4087 RVA: 0x00049D19 File Offset: 0x00048119
			public IDisposable Run()
			{
				return this.parent.source.Subscribe(this);
			}

			// Token: 0x06000FF8 RID: 4088 RVA: 0x00049D2C File Offset: 0x0004812C
			public override void OnNext(TSource value)
			{
				IEnumerable<TResult> enumerable;
				try
				{
					enumerable = this.parent.selectorEnumerableWithIndex(value, this.index++);
				}
				catch (Exception error)
				{
					this.OnError(error);
					return;
				}
				using (IEnumerator<TResult> enumerator = enumerable.GetEnumerator())
				{
					bool flag = true;
					while (flag)
					{
						flag = false;
						TResult value2 = default(TResult);
						try
						{
							flag = enumerator.MoveNext();
							if (flag)
							{
								value2 = enumerator.Current;
							}
						}
						catch (Exception error2)
						{
							try
							{
								this.observer.OnError(error2);
							}
							finally
							{
								base.Dispose();
							}
							break;
						}
						if (flag)
						{
							this.observer.OnNext(value2);
						}
					}
				}
			}

			// Token: 0x06000FF9 RID: 4089 RVA: 0x00049E30 File Offset: 0x00048230
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000FFA RID: 4090 RVA: 0x00049E68 File Offset: 0x00048268
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x04000953 RID: 2387
			private readonly SelectManyObservable<TSource, TResult> parent;

			// Token: 0x04000954 RID: 2388
			private int index;
		}
	}
}
